﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace Ellanet
{
    class vixInfoWrapper
    {
        private XmlDocument vixInfoXmlDoc;

        public vixInfoWrapper()
        {
            vixInfoXmlDoc = new XmlDocument();
            vixInfoXmlDoc.LoadXml(global::Ellanet.Properties.Resources.vixInfo_Xml);
        }

        public bool DeterminevmrunPath(ref string path, bool showError)
        {
            path = null;

            foreach (XmlNode pathNode in vixInfoXmlDoc.SelectNodes("vix/vmrun_paths/path"))
            {
                if (File.Exists(pathNode.InnerText))
                {
                    path = pathNode.InnerText;
                    return true;
                }
            }

            if ((path == null) && showError)
            {
                MessageBox.Show("Could not locate vmrun.exe.\r\rPlease ensure you have the latest version of VMware VIX API installed.", "VMware VIX API Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                VisitVIXLandingPage();
            }

            return false;
        }

        private void VisitVIXLandingPage()
        {
            DialogResult visitLandingPage = MessageBox.Show("Would you like to visit the VMware VIX Landing Page now to download the latest version of the VIX API?", "VMware VIX Landing Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (visitLandingPage == DialogResult.Yes)
            {
                Process web = new Process();
                web.StartInfo.FileName = vixInfoXmlDoc.SelectSingleNode("vix/info/landing_page").InnerText;
                web.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                web.Start();
            }
        }

        public bool CheckLatestVIXAPIInstalled(string vmrunPath)
        {
            string fileCheckPath = Path.Combine(new FileInfo(vmrunPath).DirectoryName, vixInfoXmlDoc.SelectSingleNode("vix/info/version_file_check").InnerText);

            if (File.Exists(fileCheckPath))
            {
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileCheckPath);
                int latestVersionMajor = Convert.ToInt32(vixInfoXmlDoc.SelectSingleNode("vix/info/latest_version/major").InnerText);
                int latestVersionMinor = Convert.ToInt32(vixInfoXmlDoc.SelectSingleNode("vix/info/latest_version/minor").InnerText);
                int latestVersionBuild = Convert.ToInt32(vixInfoXmlDoc.SelectSingleNode("vix/info/latest_version/build").InnerText);
                Version latest = new Version(latestVersionMajor, latestVersionMinor, latestVersionBuild);
                Version installed = new Version(fvi.FileMajorPart, fvi.FileMinorPart, fvi.FileBuildPart);

                if (installed < latest)
                {
                    MessageBox.Show(String.Format("You do not currently have the latest version of the VMware VIX API installed:\r\rInstalled:\t{0}\rLatest:\t{1}\r\rWithout the latest version, you may experience problems controlling Guests on some of the latest VMware platforms.", installed.ToString(), latest.ToString()), "VMware VIX API Out Of Date", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    VisitVIXLandingPage();
                }
            }
            else
            {
                MessageBox.Show(String.Format("Could not locate \"{0}\".\r\rPlease ensure you have the latest version of VMware VIX API installed.", fileCheckPath), "VMware VIX API Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                VisitVIXLandingPage();
            }

            return false;
        }

        public bool ImplementationDirectoriesExist(string path, bool showError)
        {
            bool allDirsExist = true;
            string missingDirectories = null;

            if (Directory.Exists(path))
            {
                foreach (XmlNode directoryNode in vixInfoXmlDoc.SelectSingleNode("vix/implementation_directories").ChildNodes)
                {
                    if (!Directory.Exists(Path.Combine(path, directoryNode.SelectSingleNode("name").InnerText)))
                    {
                        allDirsExist = false;

                        if (showError)
                        {
                            foreach (XmlNode providerNode in directoryNode.SelectSingleNode("providers").ChildNodes)
                            {
                                missingDirectories += String.Format("{0} ({1} directory)\r", providerNode.InnerText, directoryNode.SelectSingleNode("name").InnerText);
                            }
                        }
                    }
                }

                if (!allDirsExist && showError)
                {
                    MessageBox.Show(String.Format("Some directories appear to be missing from the VMware VIX API installation directory.\r\r{0}\rWithout these directories, you may experience problems controlling Guests on the VMware platforms listed above.", missingDirectories), "Missing VMware VIX Directories", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    VisitVIXLandingPage();
                }
            }
            else
            {
                allDirsExist = false;
            }

            return allDirsExist;
        }
    }
}
